package com.jacky.N叉树遍历;

import java.util.ArrayList;
import java.util.List;

public class N叉树的后序遍历_590 {
    /**
     * 从左到右，然后根
     * @param root
     * @return
     */
    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        dfs(root, res);
        return res;
    }

    public void dfs(Node root, List<Integer> res) {
        if (root == null) return;
        List<Node> children = root.children;
        if (children != null) {
            int size = children.size();
            for (int i = 0; i < size; i++) {
                //从左到右递归
                dfs(children.get(i), res);
            }
        }
        //然后把跟节点的值添加到数组队列上
        res.add(root.val);
    }
}
